package com.handong.handserialprogram.module.cofigring.config;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.constants.StateConstant;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.module.base.BaseActivity;
import com.handong.handserialprogram.module.serialport.SerialPortUtil;
import com.handong.handserialprogram.voice.MediaHelper;
import com.handong.handserialprogram.widget.IPInputView;

/**
 * Created by userKey on 2018/6/22.
 */

public class ConfigActivity extends BaseActivity<ConfigPresenter> implements ConfigView,RadioGroup.OnCheckedChangeListener{

    private TextView footRingIdTV;
    private IPInputView ipInputView;
    private EditText portET;
    private RadioGroup radioGroup;
    private RadioButton escapedRB,shortRB,longRB;

    private FootRingNewEntity entity;
    private int AlarmCode=9;

    @Override
    protected int getContentViewID() {
        return R.layout.activity_config;
    }

    @Override
    protected ConfigPresenter createPresenter() {
        return new ConfigPresenter(this);
    }

    @Override
    protected void initViewByID() {
        initHeaderLayout(false);
        setCenterTitle("脚环配置");
        setOptionText("保存");
        footRingIdTV=findView(R.id.footRingIdTV);
        ipInputView=findView(R.id.ipInputView);
        portET=findView(R.id.portET);
        radioGroup=findView(R.id.radioGroup);
        escapedRB=findView(R.id.escapedRB);
        shortRB=findView(R.id.shortRB);
        longRB=findView(R.id.longRB);
    }

    @Override
    protected void initViewDataRes() {
         String  footRingId = getIntent().getExtras().getString(KeyConstant.PERSON_RING_ID);
         entity= SerialPortUtil.getInstance(this).findEntityFromList(footRingId);
        if(entity==null){
            finish();
            showLongToast("数据异常");
            return;
        }
        radioGroup.setOnCheckedChangeListener(this);
        footRingIdTV.setText(entity.getRingID());
        mPresenter.checkFootRingData(entity);
    }


    public String getIpAdressData(){
        return ipInputView.getContentIP().replaceAll(".","")+portET.getText().toString();
    }

    public void setIpDefault(){
        if (!TextUtils.isEmpty(entity.getIp())) {
            ipInputView.setContentText(entity.getIp());
        }
        if (!TextUtils.isEmpty(entity.getPort())) {
            portET.setText(entity.getPort());
        }
    }

    public void setAlarmDefault(){
        if(entity.getVoiceNumber()==StateConstant.alarm_escaped){
            escapedRB.setChecked(true);
        }else if(entity.getVoiceNumber()==StateConstant.alarm_short){
            shortRB.setChecked(true);
        }else if(entity.getVoiceNumber()==StateConstant.alarm_long){
            longRB.setChecked(true);
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (!((RadioButton) findView(checkedId)).isChecked()) {
            return;
        }
        switch (checkedId) {
            case R.id.escapedRB://我是逃犯
                AlarmCode= StateConstant.alarm_escaped;
                MediaHelper.playRawSound(this,R.raw.escaped_alarm,null);
                break;
            case R.id.shortRB://短警报
                AlarmCode=StateConstant.alarm_short;
                MediaHelper.playRawSound(this,R.raw.short_alarm,null);
                break;
            case R.id.longRB://长警报
                AlarmCode=StateConstant.alarm_long;
                MediaHelper.playRawSound(this,R.raw.long_alarm,null);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBackLayout:
                finish();
                break;
            case R.id.headerOptionLayout://保存
               mPresenter.setFootRingAlarm(entity.getRingID(),AlarmCode);
                break;
        }
    }

}
